package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.qk.commom.Result.success;

@Slf4j

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/courses")
    public Result list(String name,Integer subject,Integer target,
                       @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Course> pageResult = courseService.list(name,subject,target,page,pageSize);
        return success(pageResult);
    }
    @DeleteMapping("/courses/{id}")
    public Result deleteById(@PathVariable Integer id){
        courseService.deleteById(id);
        return success();
    }
    @PostMapping("/courses")
    public Result addCourse(@RequestBody Course course){
        courseService.add(course);
        return success();
    }
    @GetMapping("/courses/{id}")
    public Result findById(@PathVariable Integer id){
        Course course = courseService.findById(id);
        return success(course);
    }
    @PutMapping("/courses")
    public Result updateCourse(@RequestBody Course course){
        courseService.update2(course);

        return Result.success();
    }
    @GetMapping("/courses/list")
    public Result getAll(){
        List<Course> courseList = courseService.listCourse();
        return Result.success(courseList);
    }
    @GetMapping("/courses/subject/{subject}")
    public Result findBySubject(@PathVariable Integer subject){
        List<Course> courseList = courseService.findBySubject(subject);
        return Result.success(courseList);
    }

}
